<?php

namespace FacebookAction;

require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/GraphUser.php' );
require_once( 'Facebook/GraphSessionInfo.php' );

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;

class FacebookLink {

    //----------------ham publish link len trang admin ------------------------
    public function publishLinkToAdmin($session, $message) {
        try {
            $request = new FacebookRequest(
                    $session, 'POST', '/me/feed', array(
                'message' => $message,
                    )
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }

    //----------------ham publish mot link len fan page -----------------------

    public function publishLinkToFanpage($session, $page_id, $access_token, $message) {
        try {
            $request = new FacebookRequest(
                    $session, 'POST', '/' . $page_id . '/feed', array(
                'access_token' => $access_token,
                'message' => $message,
                    )
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }

    //--------------ham publish link len group --------------------------------
    public function publishLinkToGroup($session, $group_id, $access_token, $message) {
        try {
            $request = new FacebookRequest(
                    $session, 'POST', '/' . $group_id . '/feed', array(
                'access_token' => $access_token,
                'message' => $message,
                    )
            );
            $response = $request->execute();
            $graphObject = $response->getGraphObject()->asArray();
            $my_json = json_encode((array) $graphObject);
            print_r($my_json);
            $json_return = json_decode($my_json, TRUE);
            return $json_return;
        } catch (FacebookRequestException $e) {

            echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();
        }
    }
}
